﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace _0712502_0712504_DesignPatternGame
{
    public class TerrainItemManager : MapDrawer
    {
        static public List<TerrainItem> terrainItems = new List<TerrainItem>();
        public TerrainItemManager()            
        {
        }

        public void CreateTerrainMap()
        {
            int value;
            for (int i = 0; i < this.nRow; i++)
            {
                for (int j = this.nColumn - 1; j >= 0; j--)
                {
                    value = this._mapMatrix[i, j];
                    switch (value)
                    {
                        case 0:
                            break;
                        case 1:
                            {
                                //TerrainItem item = new TerrainItem(1, GameResourceContainer.ImageManager.BrickImgs, Vector2.Zero, 1.0f, i * 27, j * 27);
                                //terrainItems.Add(item);
                                break;
                            }
                        case 2:
                            {
                                TerrainItem item = new TerrainItem(2, GameResourceContainer.ImageManager.BrickImgs, 4,
                                this.StartDrawingPoint.X + j * 27, this.StartDrawingPoint.Y + i * 27, new Point(37, 37), new Point(27, 27));
                                terrainItems.Add(item);
                                break;
                            }
                        case 3:
                            {
                                TerrainItem item = new TerrainItem(2, GameResourceContainer.ImageManager.GrassImgs, 1,
                                this.StartDrawingPoint.X + j * 27, this.StartDrawingPoint.Y + i * 27, new Point(37, 37), new Point(27, 27));
                                terrainItems.Add(item);
                                break;
                            }
                        case 4:
                            {
                                TerrainItem item = new TerrainItem(2, GameResourceContainer.ImageManager.RockImgs, 1,
                                this.StartDrawingPoint.X + j * 27, this.StartDrawingPoint.Y + i * 27, new Point(37, 37), new Point(27, 27));
                                terrainItems.Add(item);
                                break;
                            }
                    }
                }
            }
        }

        public void Update()
        {
            for (int i = 0; i < terrainItems.Count(); i++)
            {
                if (terrainItems.ElementAt(i).StateUsed == StateInUse.NotUsed)
                {
                    terrainItems.RemoveAt(i);
                    if (GlobalVariables.HaveSoundEffect)
                    {
                        //GameResourceContainer.SoundManager.CrashWall.Play();
                    }
                }
                else
                {
                    terrainItems.ElementAt(i).Update();
                }
            }
        }

        public void DrawToScreen(GameTime gameTime, SpriteBatch spriteBatch)
        {
            foreach (TerrainItem terrain in terrainItems)
            {
               if (terrain._idItem != 3)
                {
                    terrain.DrawToScreen(gameTime, spriteBatch);
                  //  spriteBatch.Draw(GameResourceContainer.ImageManager.rect, terrain.CollisionRect, Color.White);
                }
            }
        }
    }
}
